package userInterface;

import java.awt.event.MouseEvent;
import java.awt.Color;

public class CreationController
{
	public CreationController(DrawingView view, DrawingModel model, RootController root)
	{
		_model = model;
		_view = view;
		_rootController = root;
	}
	
	public void mousePressed(MouseEvent e)
	{
		createObject(_rootController.getLeftPalleteButton(), new Point(e.getX(), e.getY()));
		_pressLocation = new Point(e.getX(), e.getY());
	}

	public void mouseReleased(MouseEvent e)
	{
		addObject();
	}
	
	public void mouseDragged(MouseEvent e)
	{
		editObject(new Point(e.getX(), e.getY()));
	}

	private void createObject(String s, Point mouseLoc)
	{		
		Color c = _rootController.getSelectedColor();
		
		if (s.equals("line"))
		{
			_curObj = new Line(mouseLoc, mouseLoc, 1, c);
		}
		else if (s.equals("rectangleFull"))
		{
			_curObj = new Rectangle(mouseLoc, 0, 0, 0, c);
		}
		else if (s.equals("rectangleEmpty"))
		{
			_curObj = new Rectangle(mouseLoc, 0, 0, 1, c);
		}
		else if (s.equals("ellipseFull"))
		{
			_curObj = new Ellipse(mouseLoc, 0, 0, 0, c);
		}
		else if (s.equals("ellipseEmpty"))
		{
			_curObj = new Ellipse(mouseLoc, 0, 0, 1, c);
		}
		else if (s.equals("curve"))
		{
			_curObj = new Curve(mouseLoc, mouseLoc, mouseLoc, mouseLoc, 1, c);
		}
		else if (s.equals("text"))
		{
			_curObj = new Text(mouseLoc, 20, "", c);
		}
		else
		{
			System.out.println("trying to create something that is not valid");
			System.out.println(s);
			return;
		}
		
		_model.addDrawObject(_curObj);
	}

	private void editObject(Point mouseLoc)
	{
		switch (_curObj.getShape())
		{
		case LINE:
			((Line) _curObj).setEndPoint(mouseLoc);
			break;
		case RECTANGLE:
		{
			Rectangle rect = ((Rectangle) _curObj);
			if (mouseLoc.X > _pressLocation.X && mouseLoc.Y > _pressLocation.Y)
			{
				rect.setWidth(mouseLoc.X - _pressLocation.X);
				rect.setHeight(mouseLoc.Y - _pressLocation.Y);
			}
			else if (mouseLoc.X > _pressLocation.X && mouseLoc.Y <= _pressLocation.Y)
			{
				rect.setTopLeft(new Point(rect.getTopLeft().X, mouseLoc.Y));
				rect.setWidth(mouseLoc.X - _pressLocation.X);
				rect.setHeight(_pressLocation.Y - mouseLoc.Y);
			}
			else if (mouseLoc.X <= _pressLocation.X && mouseLoc.Y > _pressLocation.Y)
			{
				rect.setTopLeft(new Point(mouseLoc.X, rect.getTopLeft().Y));
				rect.setWidth(_pressLocation.X - mouseLoc.X);
				rect.setHeight(mouseLoc.Y - _pressLocation.Y);
			}
			else if (mouseLoc.X <= _pressLocation.X && mouseLoc.Y <= _pressLocation.Y)
			{
				rect.setTopLeft(new Point(mouseLoc.X, mouseLoc.Y));
				rect.setWidth(_pressLocation.X - mouseLoc.X);
				rect.setHeight(_pressLocation.Y - mouseLoc.Y);
			}
		}
		break;
		case ELLIPSE:
		{			
			Ellipse elip = ((Ellipse) _curObj);
			if (mouseLoc.X > _pressLocation.X && mouseLoc.Y > _pressLocation.Y)
			{
				elip.setWidth(mouseLoc.X - _pressLocation.X);
				elip.setHeight(mouseLoc.Y - _pressLocation.Y);
			}
			else if (mouseLoc.X > _pressLocation.X && mouseLoc.Y <= _pressLocation.Y)
			{
				elip.setTopLeft(new Point(elip.getTopLeft().X, mouseLoc.Y));
				elip.setWidth(mouseLoc.X - _pressLocation.X);
				elip.setHeight(_pressLocation.Y - mouseLoc.Y);
			}
			else if (mouseLoc.X <= _pressLocation.X && mouseLoc.Y > _pressLocation.Y)
			{
				elip.setTopLeft(new Point(mouseLoc.X, elip.getTopLeft().Y));
				elip.setWidth(_pressLocation.X - mouseLoc.X);
				elip.setHeight(mouseLoc.Y - _pressLocation.Y);
			}
			else if (mouseLoc.X <= _pressLocation.X && mouseLoc.Y <= _pressLocation.Y)
			{
				elip.setTopLeft(new Point(mouseLoc.X, mouseLoc.Y));
				elip.setWidth(_pressLocation.X - mouseLoc.X);
				elip.setHeight(_pressLocation.Y - mouseLoc.Y);
			}
		}
		break;
		}
	}
	
	private void addObject()
	{
	}
	
	private DrawingView _view;
	private DrawingModel _model;
	private DrawObject _curObj;
	private RootController _rootController;
	private Point _pressLocation;
}
